<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<script LANGUAGE="javascript">
function redirectPageComplete(main_id,sub_id)
{	
	alert("ลบรายการสินค้าสำเร็จ");
	location = "../../products.php?main_id="+main_id+"&sub_id="+sub_id;
}

function redirectPageIncomplete(main_id,sub_id)
{
	alert("เกิดข้อผิดพลาดในการลบข้อมูล");
	location = "../../products.php?main_id="+main_id+"&sub_id="+sub_id;
}

function redirectPageIsnull(main_id,sub_id)
{
	alert("ข้อมูลบางส่วนยังเป็นค่าว่าง กรุณาลงใหม่อีกครั้ง");
	location = "../../products.php?main_id="+main_id+"&sub_id="+sub_id;	
}

<?php
include '../../../inc/reuse/dbconfig.php';
$products_id = $_GET['products_id'];
$main_id = $_GET['main_id'];
$sub_id = $_GET['sub_id'];
if(isset($products_id)){
	$src = "../../../images/uploads/$products_id";
	if(is_dir($src)){
		$src = (substr($src, -1) != "/")? $src."/":$src;
		$opendir = opendir($src);
		while($file = readdir($opendir))
		{
			if(!in_array($file, array(".", "..")))
			{
				if(!is_dir($src.$file))
				{
					@unlink($src.$file);
				} else
				{
					remove_dir($src.$file);
				}
			}
		}
		closedir($opendir);
		@rmdir($src);
		$sql = "DELETE FROM products
				WHERE products_id=$products_id";
		if(mysql_query($sql)){
		 	echo "redirectPageComplete($main_id,$sub_id);";
		}else{	
			echo "redirectPageIncomplete($sub_id,$sub_id);";
		}	
	}		
	mysql_close();	
}
else
{
	echo "redirectPageIsnull();";
}
?>
</script>
</body>
</html>
